﻿
Public Class ImageZoom
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        img_Like.Visible = False
        img_dislike.Visible = False
        Lbl_Like_Message.Visible = False
        btn_Like_unlike.Visible = False
        rbl_like_Unlike.Visible = False
        Dim UserLike_Unlike As Integer = Like_Unlike()
        Dim Record_Count As Integer = Count()
        If Record_Count = 0 Then
            Lbl_Like_Message.Visible = True
            Lbl_Like_Message.Text = " Like? Or UnLike? "
            btn_Like_unlike.Visible = True
            rbl_like_Unlike.Visible = True
            img_Like.Visible = False
            img_dislike.Visible = False
        ElseIf UserLike_Unlike = 1 Then
            img_Like.Visible = True
            Lbl_Like_Message.Visible = True
            Lbl_Like_Message.Text = " I Like You"
            img_dislike.Visible = False
            btn_Like_unlike.Visible = False
            rbl_like_Unlike.Visible = False
        ElseIf UserLike_Unlike = 0 Then
            img_dislike.Visible = True
            Lbl_Like_Message.Visible = True
            Lbl_Like_Message.Text = " I Hate You "
            btn_Like_unlike.Visible = False
            rbl_like_Unlike.Visible = False
            img_Like.Visible = False
        Else
            img_Like.Visible = False
            img_dislike.Visible = False
            Lbl_Like_Message.Visible = False
            btn_Like_unlike.Visible = False
            rbl_like_Unlike.Visible = False
            Lbl_Like_Message.Text = " No Photos"
        End If

    End Sub
    Protected Sub btn_Like_unlike_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Like_unlike.Click
        Dim Uid As Integer
        Dim Uname As String = Page.User.Identity.Name
        Uid = CInt(userID(Uname))
        Dim GID As Integer = CInt(Request.QueryString("GID"))
        Dim U_ID As Integer = CInt(Request.QueryString("UID"))
        Dim uniID As Integer = Request.QueryString("ID")
        Dim sql As String = " INSERT INTO Photo_Likes (Gallery_ID, University_ID, Like_UnLike, User_ID) VALUES (@v1,@v2,@v3, @V4)"
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("UniConnectionString").ConnectionString)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@v1", GID)
        cmd.Parameters.AddWithValue("@v2", U_ID)
        cmd.Parameters.AddWithValue("@v3", rbl_like_Unlike.SelectedValue)
        cmd.Parameters.AddWithValue("@v4", Uid)
        conn.Open()
        Try
            If rbl_like_Unlike.SelectedItem.Selected = True Then
                cmd.ExecuteNonQuery()
                conn.Close()
                cmd.Dispose()
                conn.Dispose()
            Else
                lbl_error.Text = " *Please Select either Like Or UnLike."
            End If
        Catch
        End Try
        Response.Redirect("ImageZoom.aspx?GID=" & GID & "&Uid=" & U_ID & "")
    End Sub
    Protected Function Like_Unlike() As Integer
        Dim U_id As Integer
        Dim Uname As String = Page.User.Identity.Name
        Dim ULike As Integer
        U_id = CInt(userID(Uname))
        Dim GID As Integer = CInt(Request.QueryString("GID"))
        Dim UID As Integer = CInt(Request.QueryString("UID"))
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("UniConnectionString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select  Like_UnLike from Photo_Likes Where User_ID =" & U_id & " AND Gallery_ID = " & GID & " AND University_ID = " & UID & ""
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Try
            Dreader = IDcmd.ExecuteReader()
            Dreader.Read()
            ULike = CInt(Dreader("Like_UnLike"))

        Catch

        End Try
        Return ULike
    End Function


    Protected Function userID(ByVal uname As String) As Integer
        Dim uID As Integer
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("UniConnectionString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select aspnet_Users.UserId from aspnet_Users Where aspnet_Users.UserName = '" & uname & "'"
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Try
            Dreader = IDcmd.ExecuteReader()
            Dreader.Read()
            uID = Dreader("UserId")
        Catch
        End Try
        Return uID
        conn.Close()
        IDcmd.Dispose()
        conn.Dispose()
    End Function
    Protected Function Count() As Integer
        Dim U_id As Integer
        Dim Uname As String = Page.User.Identity.Name
        U_id = CInt(userID(Uname))
        Dim GID As Integer = CInt(Request.QueryString("GID"))
        Dim UID As Integer = CInt(Request.QueryString("UID"))
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("UniConnectionString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select Count(*) As RowCount from Photo_Likes Where User_ID =" & U_id & " AND Gallery_ID = " & GID & " AND University_ID = " & UID & ""
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Dreader = IDcmd.ExecuteReader()
        Dreader.Read()
        Dim ULike_Count As Integer = CInt(Dreader("RowCount"))
        Return ULike_Count
    End Function

End Class